package com.longe.controller;

import com.longe.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    /**这里的session只适用于当前会话**/
    /*@RequestMapping("login")
    public String index(HttpServletRequest request, User user){

        HttpSession session = request.getSession();
        session.setAttribute("user", user);

        return "home";
    }*/

    /**这里的session不局限于当前会话**/
    @RequestMapping("login")
    public String loginAgain(HttpServletRequest request, HttpServletResponse response, User user){

        HttpSession session = request.getSession();
        session.setAttribute("user", user);

        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        cookie.setMaxAge(300);
        response.addCookie(cookie);

        //输出session的id
        //System.err.println("jsessionid=" + session.getId());

        return "home";
    }
}
